﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace community
{
    public partial class familyCX : Form
    {
        string communitySQBH;
        public familyCX()
        {
            InitializeComponent();
        }

        private void familyCX_Load(object sender, EventArgs e)
        {
            checkBox1.Checked = false;
            textBox1.Text = "";
            label2.Text =Convert.ToString( 0);
            if (checkBox1.Checked == true)
                textBox1.Enabled = true;
            else
            {
                textBox1.Enabled = false;
            }
            newdatagridview("");
            familyTJHS("");
        }
        private void newdatagridview(string SQBH)//刷新 datagridview1,显示这个社区家庭信息
        {
            string str;
            if(textBox1.Text=="")
                str= "select b.FAMILYCORD_CHR as 家庭编号,a.residentname_chr as 户主姓名,b.COMMUNITYNAME_CHR as 所属社区,b.MEMBERCOUNT_INT as 家庭人口,b.HOMEADDRESS_VCHR as 家庭地址,b.FIXPHONE_VCHR as  家庭电话  from T_CS_INHABITANTRECORD_NEW as a,T_CS_FAMILYRECORD as b where b.INHABITANTCODE_CHR=a.inhabitantcode_chr ";
            else
                str = "select b.FAMILYCORD_CHR as 家庭编号,a.residentname_chr as 户主姓名,b.COMMUNITYNAME_CHR as 所属社区,b.MEMBERCOUNT_INT as 家庭人口,b.HOMEADDRESS_VCHR as 家庭地址,b.FIXPHONE_VCHR as  家庭电话  from T_CS_INHABITANTRECORD_NEW as a,T_CS_FAMILYRECORD as b where b.INHABITANTCODE_CHR=a.inhabitantcode_chr and b.COMMUNITYCODE_CHR='" + SQBH + "'";

                //str = "select b.FAMILYCORD_CHR as 家庭编号,a.residentname_chr as 户主姓名,b.COMMUNITYNAME_CHR as 所属社区,b.MEMBERCOUNT_INT as 家庭人口,b.HOMEADDRESS_VCHR as 家庭地址,b.FIXPHONE_VCHR as  家庭电话  from T_CS_INHABITANTRECORD_NEW as a,T_CS_FAMILYRECORD as b where b.INHABITANTCODE_CHR=a.inhabitantcode_chr and b.COMMUNITYCODE_CHR='" + SQBH + "'";
            SqlConnection  cn = new SqlConnection(SQL.conStr);
            cn.Open();
            SqlDataAdapter dt = new SqlDataAdapter(str, cn);
            DataSet ds = new DataSet();
            dt.Fill(ds);
            dataGridView2.DataSource = ds.Tables[0];
            dataGridView2.ColumnHeadersHeight = 39;
            dataGridView2.Columns[0].Width = 200;
            dataGridView2.Columns[1].Width = 150;
            dataGridView2.Columns[2].Width = 150;
            dataGridView2.Columns[3].Width = 150;
            dataGridView2.Columns[4].Width = 150;
            dataGridView2.Columns[5].Width = 200;
            cn.Close();

        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            dataGridView1.Visible = true;
            SqlConnection  cn = new SqlConnection(SQL.conStr);
            cn.Open();
            string sqlStr = "select COMMUNITYCODE_CHR as 社区编号,COMMUNITYNAME_VCHR as 社区名称,POSTALCODE_CHR as 邮政编码  from t_cs_community  where  CURRENTCS_INT=1";
            SqlDataAdapter dt = new SqlDataAdapter(sqlStr, cn);
            DataSet ds = new DataSet();
            dt.Fill(ds);
            dataGridView1.ColumnHeadersHeight = 39;
            this.dataGridView1.DataSource = ds.Tables[0];
            dataGridView1.Columns[0].Width = 150;
            dataGridView1.Columns[1].Width = 150;
            cn.Close();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
                textBox1.Enabled = true;
            else
            {
                textBox1.Enabled = false;
                textBox1.Text = "";
            }
        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                communitySQBH = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                //communityBH = dataGridView2.Rows[e.RowIndex].Cells[0].Value.ToString();
                //communityYZBM = dataGridView2.Rows[e.RowIndex].Cells[2].Value.ToString();
                if (textBox1.Text != null) dataGridView1.Visible = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //string str = "select b.FAMILYCORD_CHR as 家庭编号,a.residentname_chr as 户主姓名,b.COMMUNITYNAME_CHR as 所属社区,b.MEMBERCOUNT_INT as 家庭人口,b.HOMEADDRESS_VCHR as 家庭地址,b.FIXPHONE_VCHR as  家庭电话  from T_CS_INHABITANTRECORD_NEW as a,T_CS_FAMILYRECORD as b where b.INHABITANTCODE_CHR=a.inhabitantcode_chr and b.FAMILYCORD_CHR='" +  + "'";
            newdatagridview(communitySQBH);
            familyTJHS(communitySQBH);
            
        }

        private void familyTJHS(string SQBH)//统计家庭户数
        {
            SqlConnection cn = new SqlConnection(SQL.conStr);
            string sql;
            if (textBox1.Text == "")
                sql = "select count(*) from T_CS_FAMILYRECORD ";
            else
                sql = "select count(*) from T_CS_FAMILYRECORD where COMMUNITYCODE_CHR='" + SQBH + "'";
            cn.Open();
            SqlCommand cm = new SqlCommand(sql, cn);
            label2.Text =Convert.ToString( cm.ExecuteScalar());
            cn.Close();
        }
    }
}
